﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Globalization;
using System.Threading;
using System.Data.SqlClient;
using CrystalDecisions.Shared;
using CrystalDecisions.CrystalReports.Engine;
using System.IO;
using SQLExecute;



public partial class profiles_list_profiles : System.Web.UI.Page
{
    private DataSet ds;
    private DataView dv = new DataView();
   
    static Guid id,id_search;
    Profiles p = new Profiles();
    ReportDocument rptDoc = new ReportDocument();
    DateTime f, t;
    static bool bSearch = false;
    Utility ex = new Utility();
   

    protected void Page_Init(object sender, EventArgs e)
    {
        BindGrid2();
        //bS
        
    }
    protected void BindGrid2()
    {
        if (txtFrom.Text == "" && txtTo.Text == "")
        {
            ds = SqlHelper.ExecuteDataset(Common.strConnect, "Get_List_Profiles2", new object[] { null, null });
            gdProfiles.DataSource = ds.Tables[0];
            gdProfiles.DataBind();
        }
        else
        {
            f = DateTime.Parse(Common.Convert_DDMMYYYY_To_MMDDYYY(txtFrom.Text));
            t = DateTime.Parse(Common.Convert_DDMMYYYY_To_MMDDYYY(txtTo.Text));

            ds = SqlHelper.ExecuteDataset(Common.strConnect, "Get_List_Profiles2", new object[] { f, t });
            gdProfiles.DataSource = ds.Tables[0];
            gdProfiles.DataBind();
        }
    
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            if (Session["username"] != null
                && Session["fullname"] != null

                )
            {
                lUsername.Text = Session["username"].ToString();
                InitCombo();
                BindGrid2();
                bSearch = false;
            }
            else Response.Redirect("../login/login.aspx");

        }
        else if (IsCallback)
        {
            BindGrid2(); 

        }
    }
    protected override void InitializeCulture()
    {
        CultureInfo c = new CultureInfo(Thread.CurrentThread.CurrentCulture.LCID);
        c.DateTimeFormat.ShortDatePattern = "yyyy/MM/dd";
        Thread.CurrentThread.CurrentCulture = c;
        base.InitializeCulture();
    }
    protected void InitCombo()
    { 
        string sql="select profile_id,fullname from tbl_profiles order by fullname desc";

        ds = SqlHelper.ExecuteDataset(Common.strConnect, CommandType.Text, sql);
        cbFullName.TextField = "fullname";
        cbFullName.ValueField="profile_id";
        cbFullName.DataSource = ds;
        cbFullName.DataBind();

    }
    private void ClearNumberZero(DataSet ds)
    {
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            if (ds.Tables[0].Rows[i]["expectedsalary"].ToString() == "0")
            {
                ds.Tables[0].Rows[i]["expectedsalary"] = DBNull.Value;
                ds.Tables[0].Rows[i]["currency_salary"] = DBNull.Value;
            }
        }
    }

    protected void btnView_Click(object sender, EventArgs e)
    {
        if (txtFrom.Text != "" && txtTo.Text != "")
        {
            BindGrid2();
            bSearch = false;
        }
    }
    private void Del_Profile(Guid id)
    {


        if (id != null)
        {
            SqlHelper.ExecuteNonQuery(Common.strConnect, "Del_Profile", new object[] { id });

        }

    }
    protected void gdProfiles_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Delete")
        {
            id = new Guid(e.CommandArgument.ToString());
            Del_Profile(id);
            BindGrid2();
        }
    }

    protected void gdProfiles_RowCommand1(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewRowCommandEventArgs e)
    {
        /*
        string id = gdProfiles.KeyFieldName.ToString();
        if (e.CommandArgs.CommandArgument == "Delete")
        {
            id = new Guid(e.CommandArgument.ToString());
            Del_Profile(id);
            BindGrid2();
        }*/


    }
    protected void LoadImage(DataRow r, string strImageField, string strPath)
    {
        try
        {
            FileStream f = new FileStream(strPath, FileMode.Open, FileAccess.Read);
            byte[] img = new byte[f.Length];
            f.Read(img, 0, Convert.ToInt32(f.Length));
            f.Close();
            r[strImageField] = img;
        }

        catch (Exception ex)
        {
            Response.Write("<font color=red>" + ex.Message + "</font>");
        }

    }


    
    protected void gdProfiles_CustomButtonCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewCustomButtonCallbackEventArgs e)
    {
        //gdProfiles.upda
        Guid id = (Guid)gdProfiles.GetRowValues(e.VisibleIndex, "profile_id");
        //string url = "";
        if (e.ButtonID == "btnEdit")
        {

            if (IsCallback)
            {
                if (!bSearch)
                    Response.RedirectLocation = "../profiles/edit_profile_edu_en.aspx?id=" + id;
                else Response.RedirectLocation = "../profiles/edit_profile_edu_en.aspx?id=" + id_search;
            }
        
        }
        if (e.ButtonID == "btnDetail")
        {


            if (IsCallback)
            {
                if (!bSearch)

                Response.RedirectLocation = "../profiles/profile_details.aspx?id=" + id;
                else
                    Response.RedirectLocation = "../profiles/profile_details.aspx?id=" + id_search;
            }

        }
        if (e.ButtonID == "btnDelete")
        {
            
            if (IsCallback)
            {
                if (!bSearch)
                {
                    SqlHelper.ExecuteNonQuery(Common.strConnect, "Del_Profile", new object[] { id });
                    BindGrid2();
                }

                else

                {
                    SqlHelper.ExecuteNonQuery(Common.strConnect, "Del_Profile", new object[] { id_search });
                    BindGrid2();
                }
            }
        }
        if (e.ButtonID == "btnExportPDF")
        {
            if (IsCallback)
            {
                if (!bSearch)
                {

                  //  string strID = Request.QueryString["id"].ToString();
                   // Guid id = new Guid(strID);
                    
                    ex.Export2PDF2(id);
                    ClientScript.RegisterStartupScript(this.Page.GetType(), "popupOpener", "var popup=window.open('../export/profile.pdf');popup.focus();", true);
                }
                else
                {
                    ex.Export2PDF2(id_search);
                    //ClientScript.RegisterStartupScript(this.Page.GetType(), "popupOpener", "var popup=window.open('../export/profile.pdf');popup.focus();", true);
                }
            
            }
        }
       
    }
    
    protected void lnkLogout_Click(object sender, EventArgs e)
    {
        FormsAuthentication.SignOut();       
        Session.Clear();
        Response.Redirect("../login/login.aspx");
    }

   
    
    protected void gdProfiles_HtmlRowCreated(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewTableRowEventArgs e)
    {
        e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#f7f7f7';");
        if ((e.VisibleIndex % 2) == 0)
        {
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='white';");
        }
        else
        {
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='white';");
        }


    }
   

    protected void cbFullName_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (cbFullName.SelectedIndex > 0)
        {
          
            id_search=new Guid (cbFullName.SelectedItem.Value.ToString());
            Get_Details_Profiles(id_search);
            bSearch = true;
        }
    }
    protected void Get_Details_Profiles(Guid id)
    {
        ds = SqlHelper.ExecuteDataset(Common.strConnect, "Get_Profile", new object[] { id });
        gdProfiles.DataSource = ds.Tables[0];
        gdProfiles.DataBind();
        //gdProfiles.Rows
    }
    protected void btnRefresh_Click(object sender, EventArgs e)
    {
       
        cbFullName.SelectedIndex = -1;
        cbFullName.Text = "";
        txtFrom.Text = "";
        txtTo.Text = "";
        bSearch = false;
    }
}
